const keywords = ['function', 'const', '(', ')', '{', '}', '=', '.']
const identifierReg = /[a-zA-Z$_][a-zA-Z$_\d]*/
const stringLiteralReg = /('.+?')/

let parserCode = `function testa() {
    const abc = '123'
    const dfds = 'fsadf'
    console.log(abc)
}
`
const codeAry = parserCode.split(/\s/).filter(item => !!item)
const tokens = []

for (let code of codeAry) {
    while (code) {
        const keyword = isKeyword(code)
        const stringLiteralResult = code.match(stringLiteralReg)
        const identifierResult = code.match(identifierReg)
        if (keyword) {
            tokens.push({type: 'keyword', token: keyword})
            code = code.substring(keyword.length)
        } else if (stringLiteralResult && stringLiteralResult[0]) {
            tokens.push({type: 'stringLiteral', token: stringLiteralResult[0]})
            code = code.substring(stringLiteralResult[0].length)
        } else if (identifierResult && identifierResult[0]) {
            tokens.push({type: 'identifier', token: identifierResult[0]})
            code = code.substring(identifierResult[0].length)
        }
    }
}
console.log(tokens)

function isKeyword(code) {
    for (const keyword of keywords) {
        if (code.startsWith(keyword)) {
            return keyword
        }
    }
}
